package com.sofar.jwt.controller;

import com.sofar.jwt.common.Result;
import com.sofar.jwt.entity.AjaxResult;
import com.sofar.jwt.entity.TblUser;
import com.sofar.jwt.service.TblUserService;
import com.sofar.jwt.utils.JWTUtil;
import org.apache.catalina.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * (TblUser)表控制层
 *
 * @author makejava
 * @since 2022-08-12 15:43:06
 */
@RestController
@RequestMapping("tblUser")
public class TblUserController {
    /**
     * 服务对象
     */
    @Resource
    private TblUserService tblUserService;

    @PostMapping("login")
    public AjaxResult login(@RequestBody TblUser user){
        String token = JWTUtil.createToken(user);
        return Result.success(token);
    }

    @GetMapping("test")
    public AjaxResult test(HttpServletRequest request) {

        return Result.success();
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<TblUser> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.tblUserService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param tblUser 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<TblUser> add(TblUser tblUser) {
        return ResponseEntity.ok(this.tblUserService.insert(tblUser));
    }

    /**
     * 编辑数据
     *
     * @param tblUser 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<TblUser> edit(TblUser tblUser) {
        return ResponseEntity.ok(this.tblUserService.update(tblUser));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.tblUserService.deleteById(id));
    }

}

